package Interfaz;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;
import java.awt.event.KeyEvent;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Pantalla extends javax.swing.JFrame implements KeyListener{
	private JPanel panelControles;
	private JPanel panelJuego;
	private JLabel labelTCombustible;
	private JLabel labelCombustible;
	private JButton botonCargarNivel;
	private JButton botonGirarDerecha;
	private JButton botonGirarIzquierda;
	private JButton botonAvanzar;	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args)  {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Pantalla inst = new Pantalla();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Pantalla() {
		super();		
		initGUI();
		JLabel image=new JLabel();
		JLabel text=new JLabel("Press Start");
		image.setBounds(0, 0, panelJuego.getWidth(), panelJuego.getHeight());
		text.setForeground(Color.RED);
		ImageIcon img=new ImageIcon(this.getClass().getClassLoader().getResource("Imagenes/start.jpg"));		
		Icon icono = new ImageIcon(img.getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_DEFAULT));
		image.setIcon(icono);
		panelJuego.add(text);
		panelJuego.add(image);		
		
		botonAvanzar.setEnabled(false);
		botonGirarIzquierda.setEnabled(false);
		botonGirarDerecha.setEnabled(false);
		botonCargarNivel.requestFocus();
		panelJuego.addKeyListener(this);
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			FlowLayout thisLayout = new FlowLayout();
			thisLayout.setAlignment(FlowLayout.LEFT);
			getContentPane().setLayout(thisLayout);
			setResizable(false);
			this.setState(Frame.ICONIFIED);			
			{
				panelControles = new JPanel();
				getContentPane().add(panelControles);
				panelControles.setLayout(null);
				panelControles.setPreferredSize(new java.awt.Dimension(257, 608));
				panelControles.setSize(257, 600);
				{
					botonGirarIzquierda = new JButton();
					panelControles.add(botonGirarIzquierda);
					botonGirarIzquierda.setBounds(28, 462, 98, 40);
					ImageIcon img=new ImageIcon(this.getClass().getClassLoader().getResource("Imagenes/arrow_left.png"));
					Icon icono = new ImageIcon(img.getImage().getScaledInstance(botonGirarIzquierda.getWidth(), botonGirarIzquierda.getHeight(), Image.SCALE_DEFAULT));
					botonGirarIzquierda.setIcon(icono);
					botonGirarIzquierda.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							Metodos.get().girarIzquierda();
							panelJuego.requestFocus();							

						}
					});
				}
				{
					botonGirarDerecha = new JButton();
					panelControles.add(botonGirarDerecha);
					botonGirarDerecha.setBounds(147, 462, 98, 40);
					ImageIcon img=new ImageIcon(this.getClass().getClassLoader().getResource("Imagenes/arrow_right.png"));
					Icon icono = new ImageIcon(img.getImage().getScaledInstance(botonGirarDerecha.getWidth(), botonGirarDerecha.getHeight(), Image.SCALE_DEFAULT));
					botonGirarDerecha.setIcon(icono);
					botonGirarDerecha.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							Metodos.get().girarDerecha();
							panelJuego.requestFocus();
						}
					});
				}				
				
				{
					botonCargarNivel = new JButton();
					panelControles.add(botonCargarNivel);
					botonCargarNivel.setText("Start");
					botonCargarNivel.setBounds(7, 182, 245, 42);
					botonCargarNivel.setFont(new java.awt.Font("Comic Sans MS",1,20));
					botonCargarNivel.setForeground(new java.awt.Color(0,128,255));
					botonCargarNivel.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							panelJuego.removeAll();
							Metodos.get().setLabels(labelCombustible);
							Metodos.get().cargarNivel();							
							botonAvanzar.setEnabled(true);
							botonGirarIzquierda.setEnabled(true);
							botonGirarDerecha.setEnabled(true);
							panelJuego.requestFocus();
							
						}
					});
				}
				{
					botonAvanzar = new JButton();
					panelControles.add(botonAvanzar);
					botonAvanzar.setBounds(84, 392,100, 42);
					ImageIcon img=new ImageIcon(this.getClass().getClassLoader().getResource("Imagenes/arrow_up.png"));
					Icon icono = new ImageIcon(img.getImage().getScaledInstance(botonAvanzar.getWidth(), botonAvanzar.getHeight(), Image.SCALE_DEFAULT));
					botonAvanzar.setIcon(icono);
					botonAvanzar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							Metodos.get().avanzar();						
							if(Metodos.get().Terminamos())							
								terminar(Metodos.get().ganamos());							
							panelJuego.requestFocus();							
							
						}
					});
				}
				{
					labelTCombustible = new JLabel();
					panelControles.add(labelTCombustible);
					labelTCombustible.setText("Combustible :");
					labelTCombustible.setBounds(7, 21, 91, 21);
				}
				{
					labelCombustible = new JLabel();
					panelControles.add(labelCombustible);
					labelCombustible.setBounds(91, 21, 49, 14);
				}
			}
			{
				panelJuego = new JPanel();
				panelJuego.setPreferredSize(new java.awt.Dimension(800, 600));
				getContentPane().add(panelJuego);				
				panelJuego.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				
				int ancho=(800/10);
				int altura=(600/10);
				SpriteStore.get().setDimension(ancho, altura);
				Metodos.get().setDimension(ancho ,altura);
			}
			pack();
			this.setSize(1101, 668);		
			Metodos.get().setGraphics(panelJuego.getGraphics());			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	public void terminar(boolean x){		
		if(x==true){
			JLabel image=new JLabel();
			JLabel text=new JLabel("Win");
			image.setBounds(0, 0, panelJuego.getWidth(), panelJuego.getHeight());
			text.setForeground(Color.RED);
			ImageIcon img=new ImageIcon(this.getClass().getClassLoader().getResource("Imagenes/start.jpg"));		
			Icon icono = new ImageIcon(img.getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_DEFAULT));
			image.setIcon(icono);
			panelJuego.add(text);
			panelJuego.add(image);
			panelJuego.repaint();
		}else{
			JLabel image=new JLabel();
			JLabel text=new JLabel("Game Over");
			image.setBounds(0, 0, panelJuego.getWidth(), panelJuego.getHeight());
			text.setForeground(Color.RED);
			ImageIcon img=new ImageIcon(this.getClass().getClassLoader().getResource("Imagenes/game over.jpg"));		
			Icon icono = new ImageIcon(img.getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_DEFAULT));
			image.setIcon(icono);
			panelJuego.add(text);
			panelJuego.add(image);
			panelJuego.repaint();
		}
		botonAvanzar.setEnabled(false);
		botonGirarIzquierda.setEnabled(false);
		botonGirarDerecha.setEnabled(false);
		botonCargarNivel.requestFocus();
	}
	
	
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			Metodos.get().girarIzquierda();

		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			Metodos.get().girarDerecha();

		}
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			Metodos.get().avanzar();
			if(Metodos.get().Terminamos())							
				terminar(Metodos.get().ganamos());
		}
	} 
		
	public void keyReleased(KeyEvent e) {
	}
	
	public void keyTyped(KeyEvent e) {
		
	}
   
}